<template>
  <div class="edit-container">
    <van-nav-bar
      title="编辑页面"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />

    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="showClick"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="showDatePick = true"
      />
    </van-cell-group>

    <van-dialog
      v-model="show"
      title="修改昵称"
      show-cancel-button
      @confirm="updateUserInfo"
    >
      <!-- 输入任意文本 -->
      <van-field ref="nameFiled" v-model="text" label="请输入昵称" />
    </van-dialog>

    <van-popup v-model="showPopup" position="bottom">
      <!-- 输入任意文本 -->
      <van-cell is-link title="男" @click="updateGender(0)" />
      <van-cell is-link title="女" @click="updateGender(1)" />
    </van-popup>

    <van-popup position="bottom" v-model="showDatePick">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePick = false"
        @confirm="updateBirthday"
      />
    </van-popup>
    <!-- 图片裁剪盒子 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        fixed
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button @click="shbmitPhoto" class="confirm-btn" type="primary"
        >确认</van-button
      >
      <van-button @click="showCropper = false" class="cancel-btn" type="info"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { editUserInfo, editUserAvatar } from "./user.js";
import moment from "moment";
// 组件内使用;
import { VueCropper } from "vue-cropper";
export default {
  data() {
    return {
      show: false,
      text: "",
      showPopup: false,
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      showDatePick: false,
      showCropper: false, // 裁剪图片切口
      img: "", //要裁剪的图片
    };
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  components: {
    VueCropper,
  },
  methods: {
    // 姓名框弹出,赋值, 聚焦
    showClick() {
      this.show = true;
      this.text = this.userInfo.name;

      this.$nextTick(() => {
        this.$refs.nameFiled.focus();
      });
    },
    // 发起请求, 更新vuex数据
    updateUserInfo() {
      editUserInfo({ name: this.text }).then((res) => {
        console.log(res);

        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          name: this.text,
        });
      });
    },
    updateGender(gender) {
      editUserInfo({ gender }).then((res) => {
        console.log(res);
        // 更新数据
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          gender,
        });
        // 轻提示
        this.$toast.success("登陆成功!");
        // 将弹框隐藏
        this.showPopup = false;
      });
    },
    updateBirthday() {
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      editUserInfo({ birthday }).then((res) => {
        console.log(res);
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          birthday: birthday,
        });
        this.$toast.success("更新成功");
        this.showDatePick = false;
      });
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      // console.log(file);
      this.img = file.content
      this.showCropper = true
      // 相当于一步走的 , 分成两步 , 原本直接上传图片 , 现在中间是先打开裁剪盒子 , 在上传
    },
    shbmitPhoto(){
     // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(blob => {
      let fd = new FormData();
      fd.append("photo", blob);
      editUserAvatar(fd).then((res) => {
        console.log(res);
        //(1)弹窗提示
        this.$toast.success("更新成功!");
        //(2)更新vuex数据
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          photo: res.data.data.photo,
        });
        this.showCropper = false
      });
    })
    }
  },
};
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像部分
  .avatar {
    padding: 20px 0;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
  }
  //文件上传
  .van-uploader {
    opacity: 0;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    width: 120px;
    height: 120px;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
 } 
 // 裁切弹框样式
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }

</style>